Part III - More
Transformations and Programs |
Hearn and Baker – Chapter 5
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class PolyTrans
{
public
static void main(String[] args)
{
Frame
frame = new Frame("Translate 2D");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new
Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
public class cPt2D {
public float x, y;
cPt2D(float xcoord, float ycoord){
x=
xcoord;
y=
ycoord;
}
};
public void translatePolygon (GL
gl, cPt2D [] verts, int nVerts, float tx, float ty)
{
int k;
for (k = 0; k < nVerts; k++) {
verts [k].x = verts [k].x + tx;
verts [k].y = verts [k].y + ty;
}
gl.glBegin (GL.GL_POLYGON);
for (k = 0; k < nVerts; k++)
gl.glVertex2f (verts[k].x,
verts[k].y);
gl.glEnd ( );
}
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D [] polygon = new cPt2D[4];
polygon[0] = new cPt2D(-50.0f,-50.0f);
polygon[1] = new cPt2D(50.0f,-50.0f);
polygon[2] = new cPt2D(50.0f,50.0f);
polygon[3] = new cPt2D(-50.0f,50.0f);
gl.glColor3fv (CLUT[0]);
translatePolygon(gl, polygon, 4, 0, 0);
gl.glColor3fv (CLUT[2]);
translatePolygon(gl, polygon, 4, 100, -300);
gl.glColor3fv (CLUT[4]);
translatePolygon(gl, polygon, 4, 200, 400);
gl.glColor3fv (CLUT[3]);
translatePolygon(gl, polygon, 4, -400, -450);
gl.glColor3fv (CLUT[5]);
translatePolygon(gl, polygon, 4, -300, 450);
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (-500.0, 500.0, -500, 500.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class PolyTransAnim
{
static Animator animator = null;
public
static void main(String[] args)
{
Frame frame = new Frame("Translate 2D
Animate ");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
public
class cPt2D {
public
float x, y;
cPt2D(float xcoord, float ycoord){
x= xcoord;
y= ycoord;
}
};
public
void translatePolygon (GL gl, cPt2D [] verts, int nVerts, float tx, float ty)
{
int k;
for (k
= 0; k < nVerts; k++) {
verts [k].x = verts [k].x + tx;
verts [k].y = verts [k].y + ty;
}
gl.glBegin
(GL.GL_POLYGON);
for
(k = 0; k < nVerts; k++)
gl.glVertex2f (verts[k].x, verts[k].y);
gl.glEnd ( );
}
float tx = -500.0f, ty=0.0f;
float txlast = -500.0f, txdiff=0.0f;
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D []
polygon = new cPt2D[4];
polygon[0] = new cPt2D(-50.0f,-50.0f);
polygon[1] = new cPt2D(50.0f,-50.0f);
polygon[2] = new cPt2D(50.0f,50.0f);
polygon[3] = new cPt2D(-50.0f,50.0f);
translatePolygon(gl, polygon, 4, tx, ty);
txlast = tx;
tx+=2;
if ((tx <= 500.0f) && (txdiff >= 0.00f ))
{tx+=2;
gl.glColor3fv(CLUT[2]); }
else
{tx-=2; gl.glColor3fv(CLUT[4]); }
if (tx > -500.0f) tx-=2;
txdiff = tx - txlast;
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode
(GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (-500.0, 500.0, -500, 500.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
animator.stop();
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class PolyScale
{
public
static void main(String[] args)
{
Frame
frame = new Frame("Scale 2D");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public void
windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
public class cPt2D {
public float x, y;
cPt2D(float xcoord, float ycoord){
x=
xcoord;
y=
ycoord;
}
cPt2D ( ){
x =
0.0f;
y =
0.0f;
}
}
public void
translatePolygon (GL gl, cPt2D [] verts, int nVerts, float tx, float ty)
{
for
(int k = 0; k < nVerts; k++) {
verts[k].x = verts [k].x + tx;
verts[k].y = verts [k].y + ty;
}
}
public void scalePolygon (GL gl,
cPt2D [] verts, cPt2D fixedPt, int nVerts, float sx, float sy)
{
cPt2D [] vertsNew = new cPt2D[4];
for (int k = 0; k < nVerts; k++)
vertsNew[k]
= new cPt2D( );
//System.out.println (" "+
fixedPt.x + " "+ fixedPt.y);
for (int k = 0; k < nVerts; k++) {
vertsNew[k].x = verts[k].x * sx +
fixedPt.x * (1 - sx);
vertsNew[k].y = verts[k].y * sy +
fixedPt.y * (1 - sy);
}
gl.glBegin(GL.GL_POLYGON);
for (int k = 0; k < nVerts; k++)
gl.glVertex2f(vertsNew[k].x,
vertsNew[k].y);
gl.glEnd ( );
}
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final GLU
glu = gLDrawable.getGLU();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D
[] polygon = new cPt2D[4];
polygon[0] = new cPt2D(-50.0f,-50.0f);
polygon[1] = new cPt2D(50.0f,-50.0f);
polygon[2] = new cPt2D(50.0f,50.0f);
polygon[3] = new cPt2D(-50.0f,50.0f);
cPt2D
fixedPt = new cPt2D(polygon[0].x,polygon[0].y);
// System.out.println (" "+ fixedPt.x
+ " "+ fixedPt.y);
gl.glColor3fv (CLUT[0]);
scalePolygon(gl, polygon, fixedPt, 4, 3.0f, 0.5f);
gl.glColor3fv (CLUT[2]);
translatePolygon(gl, polygon, 4, -200, -400);
scalePolygon(gl, polygon, fixedPt, 4, 2.0f, 1.0f);
gl.glColor3fv (CLUT[4]);
translatePolygon(gl, polygon, 4, 600, 200);
scalePolygon(gl, polygon, fixedPt, 4, 1.0f, 2.0f);
gl.glColor3fv (CLUT[7]);
translatePolygon(gl, polygon, 4, -500, 250);
scalePolygon(gl, polygon,
fixedPt, 4, 3.0f, 3.0f);
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f,
0.0f); //set background to white
glu.gluOrtho2D (-500.0, 500.0, -500, 500.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public void
displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class PolyRot
{
public
static void main(String[] args)
{
Frame
frame = new Frame("Rotation 2D");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
public class cPt2D {
public float x, y;
cPt2D(float xcoord, float ycoord){
x=
xcoord;
y=
ycoord;
}
cPt2D( ){
x =
0.0f;
y =
0.0f;
}
};
void rotatePolygon (GL gl, cPt2D
[] verts, cPt2D pivPt, int nVerts,
float theta)
{
cPt2D [] vertsRot= new cPt2D[4];
double deg_to_rad;
deg_to_rad = Math.PI / 180.0;
for (int k = 0; k < nVerts; k++)
vertsRot[k]
= new cPt2D( );
for (int k = 0; k < nVerts; k++) {
vertsRot[k].x = pivPt.x + (verts[k].x
- pivPt.x) * (float)Math.cos(theta*deg_to_rad)
- (verts[k].y -
pivPt.y) * (float)Math.sin(theta*deg_to_rad);
vertsRot[k].y = pivPt.y + (verts
[k].x - pivPt.x) * (float)Math.sin(theta*deg_to_rad)
+ (verts[k].y -
pivPt.y) * (float)Math.cos(theta*deg_to_rad);
}
gl.glBegin(GL.GL_POLYGON);
for (int k = 0; k < nVerts; k++)
gl.glVertex2f (vertsRot[k].x,
vertsRot[k].y);
gl.glEnd ( );
}
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
float [][]
CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D [] polygon = new cPt2D[4];
float
angle=0.0f;
int i
=0;
polygon[0] = new cPt2D(-10.0f,-10.0f);
polygon[1] = new cPt2D(400.0f,-10.0f);
polygon[2] = new cPt2D(400.0f,10.0f);
polygon[3] = new cPt2D(-10.0f,10.0f);
cPt2D fixedPt = new
cPt2D(polygon[0].x,polygon[0].y);
while(angle < 360){
gl.glColor3fv (CLUT[i%8]);
rotatePolygon(gl, polygon, fixedPt, 4, angle);
i++;
angle+=30;
}
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode
(GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (-500.0, 500.0, -500, 500.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class Clock
{
static Animator animator = null;
public
static void main(String[] args)
{
Frame
frame = new Frame("Clock");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
animator = new Animator(canvas);
frame.addWindowListener(new
WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
static class Renderer implements
GLEventListener, KeyListener
{
public
class cPt2D {
public
float x, y;
cPt2D(float xcoord, float ycoord){
x= xcoord;
y= ycoord;
}
cPt2D(
){
x = 0.0f;
y = 0.0f;
}
};
void
rotatePolygon (GL gl, cPt2D [] verts, cPt2D pivPt, int nVerts,
float theta)
{
cPt2D
[] vertsRot= new cPt2D[4];
double
deg_to_rad;
deg_to_rad = Math.PI / 180.0;
for
(int k = 0; k < nVerts; k++)
vertsRot[k] = new cPt2D( );
for
(int k = 0; k < nVerts; k++) {
vertsRot[k].x = pivPt.x + (verts[k].x - pivPt.x) *
(float)Math.cos(theta*deg_to_rad)
- (verts[k].y - pivPt.y) *
(float)Math.sin(theta*deg_to_rad);
vertsRot[k].y = pivPt.y + (verts [k].x - pivPt.x) *
(float)Math.sin(theta*deg_to_rad)
+ (verts[k].y - pivPt.y) *
(float)Math.cos(theta*deg_to_rad);
}
gl.glBegin(GL.GL_POLYGON);
for
(int k = 0; k < nVerts; k++)
gl.glVertex2f (vertsRot[k].x, vertsRot[k].y);
gl.glEnd ( );
}
void buildFace (GL gl, GLUT
glut){
double angle=0.0f, r=450, ang;
int x, y, i=0;
String [] num = {"3",
"2", "1", "12", "11",
"10","9", "8", "7","6",
"5", "4"};
gl.glColor3f(1.0f,1.0f,1.0f);
while(angle < 360){
ang
= angle*Math.PI/180.0;
x = (int)( r*Math.cos(ang));
y = (int)(r*Math.sin(ang));
gl.glRasterPos2i(x,y); // set position
glut.glutBitmapString(gl, 5, num[i]);
angle+=30;
i++;
}
}
float angle=90.0f,
sec_ang=270.0f; int i =0;
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
final
GLUT glut = new GLUT();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D [] polygon1 = new
cPt2D[4];
cPt2D [] polygon2 = new
cPt2D[4];
polygon1[0] = new cPt2D(-10.0f,-10.0f);
polygon1[1] = new cPt2D(420.0f,-10.0f);
polygon1[2] = new cPt2D(380.0f,10.0f);
polygon1[3] = new cPt2D(-10.0f,10.0f);
polygon2[0] = new cPt2D(-10.0f,-10.0f);
polygon2[1] = new cPt2D(250.0f,-10.0f);
polygon2[2] = new cPt2D(180.0f,10.0f);
polygon2[3] = new cPt2D(-10.0f,10.0f);
cPt2D
fixedPt = new cPt2D(0.0f,0.0f);
buildFace(gl, glut);
int ang;
ang
= (int)(angle/45.0f);
gl.glColor3fv
(CLUT[2]);
rotatePolygon(gl,
polygon1, fixedPt, 4, angle);
gl.glColor3fv
(CLUT[5]);
rotatePolygon(gl, polygon2, fixedPt,
4, 82.5f+ angle/12.0f);
angle-=0.05f;
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (-500.0, 500.0, -500, 500.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
animator.stop();
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class GL2Dprims
{
public
static void main(String[] args)
{
Frame
frame = new Frame("GL 2d Primitive Operations");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glColor3f (1.0f, 1.0f, 1.0f);
gl.glBegin(GL.GL_LINES);
gl.glVertex2f(0.0f,-300.0f);
gl.glVertex2f(0.0f,300.0f);
gl.glVertex2f(-300.0f,0.0f);
gl.glVertex2f(300.0f,0.0f);
gl.glEnd();
gl.glLoadIdentity ( );
gl.glColor3f (0.0f, 0.0f, 1.0f);
gl.glRecti (50, 100, 200, 150); // Display blue rectangle.
gl.glColor3f (1.0f, 0.0f, 0.0f);
gl.glTranslatef (-200.0f, -150.0f, 0.0f); // Set translation parameters.
gl.glRecti (50, 100, 200, 150);
// Display red, translated rectangle.
gl.glColor3f (1.0f, 1.0f, 0.0f);
gl.glLoadIdentity ( );
// Reset current matrix to identity.
gl.glRotatef
(45.0f, 0.0f, 0.0f, 1.0f); // Set 45-deg. rotation about z axis.
gl.glRecti (50, 100, 200, 150);
// Display yellow, rotated rectangle.
gl.glColor3f (.0f, 1.0f, 0.0f);
gl.glLoadIdentity ( );
// Reset current matrix to identity.
gl.glScalef (-0.75f, -1.5f, 1.0f);
// Set scale-reflection parameters.
gl.glRecti (50, 100, 200, 150);
// Display green, transformed rectangle.
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (-300.0, 300.0, -300, 300.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public void
keyTyped(KeyEvent e) {}
}
}
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class GL3DRot
{
static Animator
animator = null;
public
static void main(String[] args)
{
Frame
frame = new Frame("OpenGl 3D Rotation ");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
animator
= new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
static
class Renderer implements GLEventListener, KeyListener
{
private
float rquad = 0.0f;
private
float rtri = 0.0f;
public void
display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT
| GL.GL_DEPTH_BUFFER_BIT); //
Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_TRIANGLES); // Drawing Using Triangles
gl.glColor3f(1.0f, 0.0f, 0.0f);
// Set the current drawing color to red
gl.glVertex3f( 0.0f, 1.0f, 0.0f); //
Top
gl.glColor3f(0.0f, 1.0f, 0.0f);
// Set the current drawing color to green
gl.glVertex3f(-1.0f,-1.0f, 0.0f); //
Bottom Left
gl.glColor3f(0.0f, 0.0f, 1.0f);
// Set the current drawing color to blue
gl.glVertex3f( 1.0f,-1.0f, 0.0f); //
Bottom Right
gl.glColor3f(1.0f, 1.0f, 0.0f);
// Set the current drawing color to yel
gl.glVertex3f( 0.0f, 1.0f, 0.0f); //
Top
gl.glColor3f(1.0f, 0.0f, 1.0f);
// Set the current drawing color to mag
gl.glVertex3f(0.0f,-1.0f, -1.0f); //
Bottom Left
gl.glColor3f(0.0f, 1.0f, 1.0f);
// Set the current drawing color to cyan
gl.glVertex3f( 0.0f,-1.0f, 1.0f); //
Bottom Right
gl.glEnd(); // Finished Drawing The Triangle
gl.glFlush();
rtri +=
0.2f;
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
gl.glClearDepth(1.0f);
// Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing
To Do
gl.glOrtho (-2.0, 2.0, -2.0, 2.0, -15.0,
15.0); // define drawing area
gLDrawable.addKeyListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if (e.getKeyCode()
== KeyEvent.VK_ESCAPE)
animator.stop();
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
}